package xin.marcher.module.diplomacy.api.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.enums.ProductTypeEnum;
import xin.marcher.module.common.exception.ProductErrorCodeEnum;
import xin.marcher.module.diplomacy.api.FrontCategoryApi;
import xin.marcher.module.diplomacy.converter.FrontCategoryConverter;
import xin.marcher.module.diplomacy.domain.bo.FrontCategoryBO;
import xin.marcher.module.diplomacy.domain.bo.FrontCategoryRelationBO;
import xin.marcher.module.diplomacy.domain.bo.SkuInfoBO;
import xin.marcher.module.diplomacy.domain.dto.FrontCategoryDTO;
import xin.marcher.module.diplomacy.domain.dto.FrontCategorySkuRelationDTO;
import xin.marcher.module.diplomacy.domain.query.FrontCategoryQuery;
import xin.marcher.module.diplomacy.manager.custom.cache.FrontCategoryCache;
import xin.marcher.module.diplomacy.manager.custom.cache.FrontCategoryRelationCache;
import xin.marcher.module.diplomacy.manager.custom.cache.SkuCollectCache;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 商品前台类目服务
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = FrontCategoryApi.class, retries = 0)
public class FrontCategoryApiImpl implements FrontCategoryApi {

    @Resource
    private FrontCategoryCache frontCategoryStringSource;

    @Resource
    private FrontCategoryRelationCache frontCategoryRelationCache;

    @Resource
    private SkuCollectCache skuCollectCache;

    @Resource
    private FrontCategoryConverter frontCategoryConverter;

    /**
     * 获取前台类目
     *
     * @param frontCategoryQuery
     */
    @Override
    public JsonResult<List<FrontCategoryDTO>> getFrontCategory(FrontCategoryQuery frontCategoryQuery) {
        // 入参校验
        if (Objects.isNull(frontCategoryQuery) || Objects.isNull(frontCategoryQuery.getFrontCategoryId())) {
            return JsonResult.buildError(ProductErrorCodeEnum.PARAM_ERROR.getErrorCode(), ProductErrorCodeEnum.PARAM_ERROR.getErrorMsg());
        }

        List<String> frontCategoryIdList = Arrays.asList(String.valueOf(frontCategoryQuery.getFrontCategoryId()));
        Optional<List<FrontCategoryBO>> optional = frontCategoryStringSource.listRedisStringData(true, frontCategoryIdList);
        if (!optional.isPresent()) {
            return JsonResult.buildSuccess();
        }
        List<FrontCategoryDTO> frontCategoryDTOList = frontCategoryConverter.converterFrontCategoryList(optional.get());
        return JsonResult.buildSuccess(frontCategoryDTOList);
    }

    /**
     * 获取前台类目下的商品列表
     *
     * @param frontCategoryQuery
     */
    @Override
    public JsonResult<FrontCategorySkuRelationDTO> getFrontCategorySkuList(FrontCategoryQuery frontCategoryQuery) {
        // 入参校验
        if (Objects.isNull(frontCategoryQuery) || Objects.isNull(frontCategoryQuery.getFrontCategoryId())) {
            JsonResult.buildError(ProductErrorCodeEnum.PARAM_ERROR.getErrorCode(), ProductErrorCodeEnum.PARAM_ERROR.getErrorMsg());
        }
        List<String> frontCategoryIdList = Arrays.asList(String.valueOf(frontCategoryQuery.getFrontCategoryId()));
        // 查询前台类目下关联的商品sku信息
        Optional<List<FrontCategoryRelationBO>> optiona = frontCategoryRelationCache.listRedisStringData(true, frontCategoryIdList);
        if (!optiona.isPresent()) {
            return JsonResult.buildSuccess();
        }
        // 填充商品的sku信息
        List<FrontCategoryRelationBO> frontCategoryRelationBOS = optiona.get();

        List<FrontCategoryRelationBO> frontSkuList = frontCategoryRelationBOS.stream()
                .filter(frontCategoryRelationBO ->
                        ProductTypeEnum.SKU.getCode().equals(frontCategoryRelationBO.getParticipateType()))
                .collect(Collectors.toList());
        List<String> skuIdList = frontSkuList.stream().map(FrontCategoryRelationBO::getParticipateId).collect(Collectors.toList());

        Optional<List<SkuInfoBO>> optional = skuCollectCache.listRedisStringData(true, skuIdList);
        if (!optional.isPresent()) {
            return JsonResult.buildSuccess();
        }
        List<Object> skuList = frontCategoryConverter.converterObjectList(optional.get());
        return JsonResult.buildSuccess(new FrontCategorySkuRelationDTO(skuList));
    }


}
